package com.xingkeduo.business.operation.domain;

import com.xingkeduo.business.operation.service.HairstyleService;
import com.xingkeduo.business.operation.service.HairstyleTagsService;
import com.xingkeduo.pojo.query.HairstyleQuery;
import com.xingkeduo.pojo.query.HairstyleTagsQuery;
import com.xingkeduo.pojo.table.HairstyleTable;
import com.xingkeduo.pojo.table.HairstyleTagsTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 标签domain
 *
 * @author chenxiangda <chenxiangda@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Component
public class HairstyleTagsDomain {

    @Autowired
    private HairstyleTagsService hairstyleTagsService;

    @Autowired
    private HairstyleService hairstyleService;

    public HairstyleTagsTable getHairstyleTagsInfoById(long id) throws Throwable {

        HairstyleTagsTable hairstyleTagsTable = hairstyleTagsService.selectById(id);

        return hairstyleTagsTable;
    }

    //创建
    public int createHairstyleTags(HairstyleTagsTable hairstyleTagsTable) throws Throwable {

        if(!CommonUtil.isEmpty(hairstyleTagsTable.getName())){
            List<HairstyleTagsTable> hairstyleTagsTables = hairstyleTagsService.selectHairstyleTagss(new HairstyleTagsQuery().setNameRepeat(hairstyleTagsTable.getName()).setType(hairstyleTagsTable.getType()));
            if(!CommonUtil.isEmpty(hairstyleTagsTables)){
                throw new DataException(DataException.Type.DEFAULT, "标签名字已经存在");
            }
        }
        int createRes = hairstyleTagsService.insert(hairstyleTagsTable);
        if (createRes > 0){
            return createRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "标签创建失败");
        }
    }

    //修改
    public int modifyHairstyleTags(HairstyleTagsTable hairstyleTagsTable) throws Throwable {
        HairstyleTagsTable hairstyleTagsTable1 = hairstyleTagsService.selectById(hairstyleTagsTable.getId());

        if(!CommonUtil.isEmpty(hairstyleTagsTable.getStatus())){
            if(hairstyleTagsTable1.getType() == 1 && hairstyleTagsTable.getStatus() ==2){
              List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setFaceTagids(String.valueOf(hairstyleTagsTable.getId())));
              if(!CommonUtil.isEmpty(hairstyleTableList)){
                  throw new DataException(DataException.Type.DEFAULT, "该脸型标签在发行库中还有绑定的发型，请先解绑!");
              }
          }else if(hairstyleTagsTable1.getType() == 2 && hairstyleTagsTable.getStatus() ==2){
              List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setJobTagids(String.valueOf(hairstyleTagsTable.getId())));
              if(!CommonUtil.isEmpty(hairstyleTableList)){
                  throw new DataException(DataException.Type.DEFAULT, "该职业标签在发行库中还有绑定的发型，请先解绑!");
              }
          }else if(hairstyleTagsTable1.getType() == 3 && hairstyleTagsTable.getStatus() ==2){
              List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setStyleTagids(String.valueOf(hairstyleTagsTable.getId())));
              if(!CommonUtil.isEmpty(hairstyleTableList)){
                  throw new DataException(DataException.Type.DEFAULT, "该风格标签在发行库中还有绑定的发型，请先解绑!");
              }
            }
        }
        if(!CommonUtil.isEmpty(hairstyleTagsTable.getName())){
            if(!hairstyleTagsTable.getName().equals(hairstyleTagsTable1.getName())){
                if(!CommonUtil.isEmpty(hairstyleTagsTable.getName())){
                    List<HairstyleTagsTable> hairstyleTagsTables = hairstyleTagsService.selectHairstyleTagss(new HairstyleTagsQuery().setNameRepeat(hairstyleTagsTable.getName()).setType(hairstyleTagsTable.getType()));
                    if(!CommonUtil.isEmpty(hairstyleTagsTables)){
                        throw new DataException(DataException.Type.DEFAULT, "发型标签名字已经存在");
                    }
                }

            }
        }


        int modifyRes = hairstyleTagsService.update(hairstyleTagsTable);
        if (modifyRes > 0){
            return modifyRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "标签修改失败");
        }
    }

    //删除
    public int delHairstyleTags(Long id) throws Throwable {

        if(!CommonUtil.isEmpty(id)){

            HairstyleTagsTable hairstyleTagsTable = hairstyleTagsService.selectById(id);

            if(hairstyleTagsTable.getType() == 1){
                List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setFaceTagids(String.valueOf(hairstyleTagsTable.getId())));
                if(!CommonUtil.isEmpty(hairstyleTableList)){
                    throw new DataException(DataException.Type.DEFAULT, "该脸型标签在发行库中还有绑定的发型，请先解绑!");
                }
            }else if(hairstyleTagsTable.getType() == 2){
                List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setJobTagids(String.valueOf(hairstyleTagsTable.getId())));
                if(!CommonUtil.isEmpty(hairstyleTableList)){
                    throw new DataException(DataException.Type.DEFAULT, "该职业标签在发行库中还有绑定的发型，请先解绑!");
                }
            }else if(hairstyleTagsTable.getType() == 3){
                List<HairstyleTable> hairstyleTableList= hairstyleService.selectHairstyles(new HairstyleQuery().setStyleTagids(String.valueOf(hairstyleTagsTable.getId())));
                if(!CommonUtil.isEmpty(hairstyleTableList)){
                    throw new DataException(DataException.Type.DEFAULT, "该风格标签在发行库中还有绑定的发型，请先解绑!");
                }
            }
        }
        HairstyleTagsTable hairstyleTagsTable = new HairstyleTagsTable();
        hairstyleTagsTable.setId(id);
        hairstyleTagsTable.setIsDelete(YesNoEnum.YES);
        int delRes = hairstyleTagsService.delete(hairstyleTagsTable);
        if(delRes > 0){
            return delRes;
        }else{
            throw new DataException(DataException.Type.FAIL, "删除失败");
        }
    }

    //获取列表
    public List<HairstyleTagsTable> getHairstyleTagslists(HairstyleTagsQuery hairstyleTagsQuery) throws Throwable {
        //查询已开启的
        hairstyleTagsQuery.setStatus(1);
        List<HairstyleTagsTable> hairstyleTagsList = hairstyleTagsService.selectHairstyleTagss(hairstyleTagsQuery);

        return hairstyleTagsList;
    }

    //分页查询
    public Page<HairstyleTagsTable> getHairstyleTagsPageList(HairstyleTagsQuery hairstyleTagsQuery) throws Throwable {
        Integer pageNo = hairstyleTagsQuery.getPageNo();
        Integer pageSize = hairstyleTagsQuery.getPageSize();
        Page<HairstyleTagsTable> hairstyleTagsTablePage = hairstyleTagsService.selectPageLists(hairstyleTagsQuery,pageNo,pageSize);
        return hairstyleTagsTablePage;
    }

}
